from flask import Flask, render_template
import json

app = Flask(__name__)

mdb = json.loads(open('yuwen.json', 'r', encoding='utf-8').read())


def find(key):
    '''查找字符串'''
    x = []
    for k, v in mdb.items():
        if not type(v) == dict:
            continue
        if v.get("title", "").find(key) != -1:
            x.append(k)
        if v.get("a", "").find(key) != -1:
            x.append(k)
        if v.get("b", "").find(key) != -1:
            x.append(k)
        if v.get("c", "").find(key) != -1:
            x.append(k)
        if v.get("d", "").find(key) != -1:
            x.append(k)
    return set(x)


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


@app.route('/index')
def index():  # put application's code here
    return render_template('index.html', name='World')


@app.route('/search/<key>')
def search(key):  # put application's code here
    k = find(key)
    p = [mdb[i] for i in k]
    return render_template('search.html', name=key, data=p)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=True)
